<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI 图像与问题解析 Demo</title>
    <style>
        body {
            font-family: 'Helvetica Neue', sans-serif;
            background-color: #f0f2f5;
            margin: 0;
            padding: 0;
        }

        header {
            background-color: #007bff;
            color: #fff;
            text-align: center;
            padding: 30px 0;
            font-size: 1.5em;
            font-weight: bold;
        }

        main {
            max-width: 800px;
            margin: 30px auto;
            padding: 20px;
            background-color: #fff;
            border-radius: 10px;
            box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
        }

        h2 {
            font-size: 1.2em;
            color: #333;
            margin-bottom: 10px;
        }

        p {
            font-size: 1em;
            color: #555;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            font-size: 0.9em;
            color: #555;
        }

        input[type="text"],
        input[type="file"] {
            width: 100%;
            padding: 10px;
            margin-top: 5px;
            font-size: 1em;
            border: 1px solid #ddd;
            border-radius: 5px;
            background-color: #f9f9f9;
            box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.05);
        }

        button {
            display: inline-block;
            background-color: #007bff;
            color: #fff;
            padding: 10px 20px;
            font-size: 1em;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        button:hover {
            background-color: #0056b3;
        }

        .image-preview {
            text-align: center;
            margin-top: 20px;
        }

        .image-preview img {
            max-width: 100%;
            height: auto;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }

        .result {
            margin-top: 20px;
            padding: 15px;
            background-color: #e9f5ff;
            border-left: 5px solid #007bff;
            border-radius: 5px;
            font-family: monospace;
            color: #333;
        }
    </style>
    <script>
        // 预览上传的图片
        function previewImage(event) {
            let reader = new FileReader();
            reader.onload = function () {
                let output = document.getElementById('image-preview-output');
                output.src = reader.result;
                document.getElementById('image-preview').style.display = 'block';
            }
            reader.readAsDataURL(event.target.files[0]);
        }

        // 发送图像和/或问题请求到Flask服务器
        function analyzeImageAndRunApi() {
            let formData = new FormData();
            let imageFile = document.getElementById("image-upload").files[0];
            let userQuery = document.getElementById("user-query").value;

            if (!userQuery) {
                document.getElementById("final-response").innerText = '请填写问题内容！';
                return;
            }

            if (imageFile && userQuery) {
                formData.append("image", imageFile);
                formData.append("query", userQuery);
                fetch('/process-image-and-query', {
                    method: 'POST',
                    body: formData
                })
                    .then(response => response.json())
                    .then(data => {
                        document.getElementById("final-response").innerText = JSON.stringify(data.answer, null, 4);
                    })
                    .catch(error => {
                        console.error('Error:', error);
                        document.getElementById("final-response").innerText = '请求出错，请重试！';
                    });
            }
            else if (userQuery) {
                let queryData = { query: userQuery };
                fetch('/ask-question', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(queryData)
                })
                    .then(response => response.json())
                    .then(data => {
                        document.getElementById("final-response").innerText = JSON.stringify(data.answer, null, 4);
                    })
                    .catch(error => {
                        console.error('Error:', error);
                        document.getElementById("final-response").innerText = '请求出错，请重试！';
                    });
            }
        }
    </script>
</head>

<body>
    <header>AI 图像与问题解析 Demo</header>

    <main>
        <h2>上传图片并输入问题</h2>
        <p>您可以上传一张图片并提问，AI 会帮助分析和解答。</p>

        <div class="form-group">
            <label for="image-upload">选择图片:</label>
            <input type="file" id="image-upload" accept="image/*" onchange="previewImage(event)">
        </div>

        <div class="form-group">
            <label for="user-query">输入问题:</label>
            <input type="text" id="user-query" placeholder="例如：图片中是什么？">
        </div>

        <button onclick="analyzeImageAndRunApi()">提交</button>

        <div class="image-preview" id="image-preview" style="display: none;">
            <img id="image-preview-output" src="" alt="图片预览">
        </div>

        <div class="result" id="final-response">等待输入...</div>
    </main>
</body>

</html>